package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.mapper.ProjecttypeMapper;
import com.hbnu.system.model.entity.Project_Type;
import com.hbnu.system.model.vo.ChildrenNameId;
import com.hbnu.system.model.vo.NameId;
import com.hbnu.system.service.IProjectTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectTypeServiceImpl extends ServiceImpl<ProjecttypeMapper, Project_Type> implements IProjectTypeService {
    @Autowired
    private IProjectTypeService iProjectTypeService;

    @Override
    public List<ChildrenNameId> getCascadingData() {
        // 一次性查询所有数据
        List<Project_Type> projectTypes = baseMapper.selectList(Wrappers.lambdaQuery(Project_Type.class));

        // 构建数据结构
        Map<Integer, String> type1Map = new HashMap<>();  // type1ID -> projectType1
        Map<Integer, String> type2Map = new HashMap<>();  // type2ID -> projectType2
        Map<Integer, Integer> connectionMap = new HashMap<>(); // type2ID -> connectionID1

        // 填充映射关系
        for (Project_Type projectType : projectTypes) {
            type1Map.put(projectType.getType1ID(), projectType.getProjectType1());
            type2Map.put(projectType.getType2ID(), projectType.getProjectType2());
            connectionMap.put(projectType.getType2ID(), projectType.getConnectionID1());
        }

        // 构建type1节点列表
        List<ChildrenNameId> type1List = new ArrayList<>();
        Map<Integer, ChildrenNameId> type1MapById = new HashMap<>();

        for (Map.Entry<Integer, String> entry : type1Map.entrySet()) {
            ChildrenNameId childrenNameId = new ChildrenNameId(entry.getValue(), entry.getKey());
            type1List.add(childrenNameId);
            type1MapById.put(entry.getKey(), childrenNameId);
        }

        // 构建type2节点列表
        List<NameId> type2List = new ArrayList<>();
        Map<Integer, NameId> type2MapById = new HashMap<>();

        for (Map.Entry<Integer, String> entry : type2Map.entrySet()) {
            NameId nameId = new NameId(entry.getValue(), entry.getKey());
            type2List.add(nameId);
            type2MapById.put(entry.getKey(), nameId);
        }

        // 建立父子关系
        for (NameId nameId : type2List) {
            Integer type2Id = nameId.getValue();
            Integer connectionId = connectionMap.get(type2Id);

            if (connectionId != null && type1MapById.containsKey(connectionId)) {
                ChildrenNameId parent = type1MapById.get(connectionId);
                parent.getChildren().add(nameId);
            }
        }

        return type1List;
    }


    @Override
    public List<ChildrenNameId> getPorCascadingData() {
        List<ChildrenNameId> cascadingData = iProjectTypeService.getCascadingData();
        List<ChildrenNameId> result = new ArrayList<>();

        for (ChildrenNameId childrenNameId: cascadingData) {

            if(childrenNameId.getValue() == 1){
                result.add(childrenNameId);
            }

        }
        return result;
    }

    @Override
    public List<ChildrenNameId> getTranCascadingData() {
        List<ChildrenNameId> cascadingData = iProjectTypeService.getCascadingData();
        List<ChildrenNameId> result = new ArrayList<>();

        for (ChildrenNameId childrenNameId: cascadingData) {

            if(childrenNameId.getValue() == 2){
                result.add(childrenNameId);
            }

        }
        return result;
    }
}
